// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-08 15:25
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.TREE;

import com.DataStruct.Tree.TwoBinaryTree.TreeNode;

public class MirrorTree {
    /**
     * 根据给定二叉树输出镜像二叉树
     */
    public TreeNode mirrorTree(TreeNode root) {
        // 遍历树的同时创建一个镜像树
        return constructMirrorTree(root);
    }

    /**
     * 遍历二叉树的同时创建一棵镜像二叉树
     *
     * @param root 根节点
     * @return 返回值
     */
    private TreeNode constructMirrorTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode node = new TreeNode(root.val);
        if (root.left != null) {
            // 原树的左节点是镜像树的右节点
            node.right = constructMirrorTree(root.left);
        }
        if (root.right != null) {
            // 原树的左节点是镜像树的右节点
            node.left = constructMirrorTree(root.right);
        }
        return node;
    }
}
